//
//  MerchantsViewController.m
//  ksjv2
//
//  Created by mac on 14/12/23.
//  Copyright (c) 2014年 CnbizMedia. All rights reserved.
//
//---------------------------------------------------------
//                    *              *
//                  *      招商详细     *
//                    *              *
//---------------------------------------------------------
#import "MerchantsViewController.h"
#import "Util.h"
//#import "OpinionViewController.h"
@interface MerchantsViewController ()<UIWebViewDelegate>{
    NSString*phoneNumber;
    UIWebView*phoneCallWebView;
}

@property (weak, nonatomic) IBOutlet UIWebView *WebView;
@property (weak, nonatomic) IBOutlet UILabel *BriefLabel;
@property (strong, nonatomic) UIView *bar;
@property (weak, nonatomic) IBOutlet UIScrollView *MainScroll;
@end

@implementation MerchantsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=@"项目介绍";
    self.view.backgroundColor=UIColorFromRGB(0xf5f5f5);
    [self initBar];
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    self.WebView.delegate=self;
    phoneNumber=self.Merchants_Dic[@"xmtel"];
    NSURL*url=[NSURL URLWithString:self.Merchants_Dic[@"url"]];
    [self.WebView loadRequest:[NSURLRequest requestWithURL:url]];
    
//    [self setBrief:@"大家想不想在自己的应用中拨打电话呀？打电话可以用openURL:这个API, 如：[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@\"tel://10086\"]];但是当电话结束后，返回的是系统的拨打电话界面，如何才能返回自己的应用呢？这儿有两种方法与大家分享。"];

}
-(void)viewDidDisappear:(BOOL)animated{
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
-(void)initBar{
    self.bar=[[UIView alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height-45, self.view.frame.size.width, 45)];
    self.bar.backgroundColor=[UIColor colorWithWhite:0.110 alpha:1.000];
    [self.view addSubview:self.bar];
    UIButton*phone=[UIButton buttonWithType:UIButtonTypeCustom];
    [phone setTitle:@"电话咨询" forState:UIControlStateNormal];
    [phone setBackgroundColor:[UIColor redColor]];
    [phone setFrame:CGRectMake(15, 8, (self.view.frame.size.width-45)/2, 30)];
    [phone addTarget:self action:@selector(phone:) forControlEvents:UIControlEventTouchUpInside];
    [self.bar addSubview:phone];
    
    UIButton*comm=[UIButton buttonWithType:UIButtonTypeCustom];
    [comm setTitle:@"留言" forState:UIControlStateNormal];
    [comm setBackgroundColor:[UIColor colorWithWhite:0.263 alpha:1.000]];
    [comm setFrame:CGRectMake((self.view.frame.size.width-45)/2+30, 8, (self.view.frame.size.width-45)/2, 30)];
    [comm addTarget:self action:@selector(message:) forControlEvents:UIControlEventTouchUpInside];
    [self.bar addSubview:comm];
}
-(void)viewDidLayoutSubviews{
    CGRect frame = self.view.frame;
    self.WebView.frame=CGRectMake(0,0,frame.size.width,frame.size.height-self.bar.frame.size.height);
//    self.MainScroll.frame=CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, frame.size.height);
    self.bar.frame=CGRectMake(0,frame.size.height-self.bar.frame.size.height,  frame.size.width, self.bar.frame.size.height);
}
-(void)loadNews{

    [[AppAPIClient sharedClient] GET:@"Jiameng/Show" parameters:@{@"id":@"1"} success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSMutableDictionary *result = responseObject[@"data"];
        if([result isKindOfClass:[NSNull class]] || [result count] == 0){
            return;
        }

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

    }];
}
-(void)viewWillDisappear:(BOOL)animated{
    [SVProgressHUD dismiss];
}
- (void)message:(id)sender {
    [self performSegueWithIdentifier:@"mPopinion" sender:nil];
    
}
- (void)phone:(id)sender {
    NSURL *phoneURL = [NSURL URLWithString:[NSString stringWithFormat:@"tel:%@",phoneNumber]];
    if ( !phoneCallWebView ) {
        phoneCallWebView = [[UIWebView alloc] initWithFrame:CGRectZero];
    }
    [phoneCallWebView loadRequest:[NSURLRequest requestWithURL:phoneURL]];
}

-(void)setMainScrollContenSize{

    if (self.view.frame.size.height<(self.BriefLabel.frame.origin.y+self.BriefLabel.frame.size.height+self.bar.frame.size.height)) {
        [self.MainScroll setContentSize:CGSizeMake(self.view.frame.size.width, self.BriefLabel.frame.origin.y+self.BriefLabel.frame.size.height)];
    }else{
        [self.MainScroll setContentSize:CGSizeMake(self.view.frame.size.width, self.MainScroll.frame.size.height)];
    }
    
}
-(void)setBrief:(NSString*)text{
    if (text==nil) {
        NSLog(@"BriefLabel.text为空");
        return;
    }
    [self.BriefLabel setNumberOfLines:0];
    self.BriefLabel.lineBreakMode = NSLineBreakByWordWrapping;
    self.BriefLabel.text=text;
    UIFont *font = [UIFont systemFontOfSize:15];
    self.BriefLabel.font=font;
    //设置一个行高上限
    CGSize Maxsize = CGSizeMake(self.view.frame.size.width,20000);
    //计算实际frame大小，并将label的frame变成实际大小
    CGRect size = [text boundingRectWithSize:Maxsize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: font} context:nil];
    [self.BriefLabel setFrame:CGRectMake(self.BriefLabel.frame.origin.x, self.BriefLabel.frame.origin.y, size.size.width,size.size.height)];
    [self setMainScrollContenSize];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
//- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

//    UIViewController *destination = segue.destinationViewController;
//    if([destination isKindOfClass:[OpinionViewController class]]){
//        [destination setValue:@"merchants" forKey:@"Source"];
//    }
//}
- (void) webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
    [SVProgressHUD showErrorWithStatus:LOADING_TEXT_ERROR];
}

- (void)webViewDidStartLoad:(UIWebView *)webView{
    
    //[SVProgressHUD showWithStatus:LOADING_TEXT maskType:SVProgressHUDMaskTypeGradient];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView{
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    
}


@end
